var api = require('../../utils/api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showbtn:1,
        tops: 32,
        users:[],
        msg:'',
        starttime:'',
        starttime1:'',
        endtime:'',
        endtime1:'',
        images:[],
        orders:[],
        orders_t:'',
        orders_id:'',
        icons:[],
        icon_t:'',
        icon_id:''
    },


    uploadpdf(){

        let that = this;
        var usrl=api.HOST;
        var images=this.data.images;
  

        wx.chooseMedia({
            count: 1,
            mediaType: ['image','video'],
            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) { 
                console.log(res.tempFiles);
                let tempFilePaths = res.tempFiles;
                console.log(tempFilePaths[0].tempFilePath);
                tempFilePaths.forEach(function(item){
                    wx.uploadFile({
                        filePath: item.tempFilePath,
                        name: 'file',
                        url: usrl+'/api/common/upload',
                        formData:{},
                        success:function(res){
                          var data= JSON.parse(res.data);
      
                          console.log(data);
                          
                          images.push(data.data.img)
                          that.setData({
                            images:images
                        })
                  
                        },
                        fail: function (res) {
                          console.log('上传失败');
                          }
                      })


                })

             

            }

        })
    },

    getmessage(ret){
        var msg=ret.detail.value;
        this.setData({
            msg:msg
        })
    },

    bindstardate1(ret){
        console.log(ret);

        var msg=ret.detail.value;
        this.setData({
            starttime:msg
        })
    },
    bindstardate2(ret){
        console.log(ret);

        var msg=ret.detail.value;
        this.setData({
            starttime1:msg
        })
    },

    bindenddate1(ret){
        console.log(ret);

        var msg=ret.detail.value;
        this.setData({
            endtime:msg
        })
    },
    bindenddate2(ret){
        console.log(ret);

        var msg=ret.detail.value;
        this.setData({
            endtime1:msg
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that=this;

        var infos = wx.getSystemInfoSync();
        console.log(infos);
        console.log(infos.statusBarHeight);

        this.setData({
            tops: infos.statusBarHeight + 4
        })

        //关联项目
        api.post({
            url: 'icons/getorder',
            data: '',
            success: data => {
                that.setData({
                    orders:data.data
                })
            }
        })

        //标签
        api.post({
            url: 'icons/getallicons',
            data: '',
            success: data => {
                that.setData({
                    icons:data.data
                })
            }
        })
    },

    getallicons(){
        wx.navigateTo({
        url: '/pages/note/icons',
        })
    },
    getusers(){
        wx.navigateTo({
          url: '/pages/project/list',
        })
    },

    onShow(){
        var that=this;

        console.log('---------',this.data.icon_id) 
        
        var all=wx.getStorageSync('all');

        if(all){
            this.setData({
                users:all
            })
        }
      
        console.log(all);

    },

    goindex() {
        wx.navigateBack();
    },
    bindorder(ret){

        console.log(ret);
        var index=ret.detail.value;
        var orders=this.data.orders;
        this.setData({
            orders_id:orders[index].id,
            orders_t:orders[index].order
        })

    },

    selcticon(ret){

        console.log(ret);


        this.setData({
            icon_id:ret.currentTarget.dataset.id,
            icon_t:ret.currentTarget.dataset.name
        })
    },

    deleteimg(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;

        var images=this.data.images;
        var imgs=[];

        images.forEach(function(item,index){
            console.log(index);
            if(index!=i){
                imgs.push(item)
            }
        })

        console.log(imgs);

        this.setData({
            images:imgs
        })

    },

    // 提交信息

    submits(){

        var that=this;
        this.setData({
            showbtn:0
        })

        wx.showLoading({
            title:'提交...',
        })

        var images=this.data.images;
        var msg=this.data.msg;
        var starttime=this.data.starttime;
        var starttime1=this.data.starttime1;
        var endtime=this.data.endtime;
        var endtime1=this.data.endtime1;

        var orders_id=this.data.orders_id;
        var icon_id=this.data.icon_id;

        var users=this.data.users;
        var userid='';

        
        users.forEach(function(item){
            console.log(item)
            userid=userid+item.id+','
        })

          //标签
          api.post({
            url: 'icons/submits',
            data: {images:images,msg:msg,starttime:starttime+' '+starttime1,endtime:endtime+' '+endtime1,orders_id:orders_id,icon_id:icon_id,userid:userid},
            success: data => {

                wx.hideLoading();
                this.setData({
                    showbtn:1
                })

                console.log(data)

                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })

                if(data.code==1){
                    wx.reLaunch({
                      url: '/pages/note/index',
                    })
                }

            }
        })


    }

})